package com.example.dragon.adpter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dragon.bean.OrderBean;
import com.example.dragon.common.Common;
import com.example.dragon.dialog.MessageDialog;
import com.example.dragon.dialog.WaitDialog;
import com.example.dragon.untis.CircleImageView;
import com.example.dragon.untis.Utils;
import com.example.dragon.xmf.CommentActivity;
import com.example.dragon.xmf.MainActivity;
import com.example.dragon.xmf.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * Created by Dragon on 2017/6/24.
 */

public class CompletedAdpter extends RecyclerView.Adapter {
    private List<OrderBean> mcomList = new ArrayList<>();
    Context mcontext;
    private MessageDialog messageDialog;
    WaitDialog mwaitDialog;
    public CompletedAdpter(Context mcontext, List<OrderBean> mcomList) {
        this.mcontext = mcontext;
        this.mcomList=mcomList;
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private PdDriverAdpter.OnItemClickLitener mOnItemClickLitener;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mcontext).inflate(R.layout.completed_item_layout, parent, false);
        mwaitDialog = new WaitDialog(mcontext,"2");
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final OrderBean item = mcomList.get(position);

        final ViewHolder viewHolder = (ViewHolder) holder;
        if (item != null) {
            String money = item.getFee();
            money = money.substring(0,money.length()-3);
            double ovtime  = Utils.getoverTIme(item.getTime4(),item.getAddtime());
            if (ovtime>2){
                double time = ovtime-2;
                int moeny  = Utils.getchaoshiTime(time)*3+50;
                viewHolder.cmfare.setText(moeny);
            }else {
                viewHolder.cmfare.setText("50");
            }
            int fee = Integer.parseInt(item.getFare())+Integer.parseInt(money);
            Log.i("money",money+"----"+item.getFare()+"--"+fee);
            viewHolder.cmoid.setText(item.getOrders_id());
            if ("0".equals(item.getIscomment())){
                viewHolder.canlebtn.setText("去评价");
                viewHolder.canlebtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(mcontext, CommentActivity.class);
                        intent.putExtra("avter",item.getAvatar());
                        intent.putExtra("dname",item.getDname());
                        intent.putExtra("orderId",item.getOrders_id());
                        mcontext.startActivity(intent);
                    }
                });
            }else {
                viewHolder.canlebtn.setText("已评价");
            }
            viewHolder.cmdname.setText(item.getDname());
            viewHolder.cmdate.setText(item.getWorktime());
            viewHolder.cmaddres.setText(item.getAddress());
            viewHolder.ratingBar.setRating(Integer.parseInt(item.getStars()));
            Glide.with(mcontext).load(item.getAvatar()).into(viewHolder.cmavater);
            if (mOnItemClickLitener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = viewHolder.getLayoutPosition();
                        mOnItemClickLitener.onItemClick(viewHolder.itemView, pos);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = viewHolder.getLayoutPosition();
                        mOnItemClickLitener.onItemLongClick(viewHolder.itemView, pos);
                        return false;
                    }
                });
            }        }
    }

    private void ommentOrder(String oid,String did){
        mwaitDialog.show();
        OkHttpUtils.post()
                .url(Common.MIFENG_URL+"Orders&a=selDriver")
                .addParams("token", MainActivity.TOKEN)
                .addParams("orders_id",oid)
                .addParams("driver_id",did)
                .build()//
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.i("res",e.toString());
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        mwaitDialog.dismiss();
                        Log.i("res",response.toString());
                        try {
                            JSONObject jsonObject =new JSONObject(response);
                            int code = jsonObject.getInt("code");
                            if (code==200){
                                messageDialog = new MessageDialog("选择司机",jsonObject.getString("message"),"确定",mcontext);
                                notifyDataSetChanged();
                            }else {
                                messageDialog = new MessageDialog("选择司机",jsonObject.getString("message"),"确定",mcontext);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public int getItemCount() {
        return mcomList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView cmoid,cmaddres,cmdate,cmfare,cmdname;
        CircleImageView cmavater;
        Button canlebtn;
        RatingBar ratingBar;
        public ViewHolder(View itemView) {
            super(itemView);
            ratingBar = (RatingBar)itemView.findViewById(R.id.comrating);
            cmoid = (TextView)itemView.findViewById(R.id.cmoidText);
            cmavater = (CircleImageView)itemView.findViewById(R.id.cmavter);
            cmdate =(TextView)itemView.findViewById(R.id.cmdateText);
            cmaddres =(TextView)itemView.findViewById(R.id.cmaddressText);
            cmfare =(TextView)itemView.findViewById(R.id.cmfareText);
            cmdname =(TextView)itemView.findViewById(R.id.cmdnameText);
            canlebtn = (Button) itemView.findViewById(R.id.cmcanrbtn);
        }
    }
}
